﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class GioHang : BasePage
    {
        ShopingCart vpp = new ShopingCart();
        private static DataTable tb = new DataTable();
        private static int[] idgh = new int[100];
        private static int tong = 0;
        private float total = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGioHang();

            }
        }
        public void Calc(object sender, EventArgs e)
        {
            TextBox tb1 = sender as TextBox;
            GridViewRow row = (GridViewRow)tb1.NamingContainer;
            string strCommissionPayoutGross = (row.FindControl("txtSL") as TextBox).Text;
            Label DG = (Label)row.FindControl("lbDG");
            Label GG = (Label)row.FindControl("lbDGG");

            decimal sum = 0;
            decimal totalMoney = 0;

            int sl = int.Parse(tb1.Text);
            int gg = int.Parse(GG.Text.Replace("%", "").Trim());
            decimal dg = 0;
            Label TT = (Label)row.FindControl("lbThanhTien");
            decimal ThanhTien = 0;
            decimal TotalMoney = 0;
            if (Session["lang"].ToString() == "VN")
            {
                dg = Decimal.Parse(DG.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                ThanhTien = (decimal)sl * (dg - ((dg * gg) / 100));
                TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## VNÐ");
            }
            if (Session["lang"].ToString() == "EN")
            {
                dg = Decimal.Parse(DG.Text.Replace(" USD", "").Replace(".", "").Trim());
                TotalMoney = (decimal)sl * (dg - ((dg * gg) / 100));
                TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## USD");
            }

            Decimal t = 0;
            Decimal tt = 0;

            for (int i = 0; i < GridGioHang.Rows.Count; i++)
            {
                Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                if (Session["lang"].ToString() == "VN")
                {
                    t = Decimal.Parse(TT1.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                }
                if (Session["lang"].ToString() == "EN")
                {
                    tt = Decimal.Parse(TT1.Text.Replace(" USD", "").Replace(".", "").Trim());
                }
                sum += t;
                totalMoney += tt;
            }
            if (Session["lang"].ToString() == "VN")
            {

                DG.Text = dg.ToString("##,## VNÐ");
                lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
            }
            if (Session["lang"].ToString() == "EN")
            {
                DG.Text = dg.ToString("##,## USD");
                lblSum.Text = "Total money: <span style='color:red;font-weight:bold;'>" + totalMoney.ToString("##,## USD") + "</span>";
            }

            tb = vpp.upDateSL(tb, Int32.Parse(((Label)row.FindControl("lbSTT")).Text), sl, ThanhTien, TotalMoney);
        }

        protected void loadGioHang()
        {
            Page.Title = new sysConfigBL().Select(new sysConfigDO { ConfigID = 1 }).ConfigValue.ToString();

            if (Session["GioHang"] != null)
            {
                tb = (DataTable)Session["GioHang"];
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("SanPhamID");
                dt.Columns.Add("TenSanPham");
                dt.Columns.Add("HinhAnh");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("DonGia");
                dt.Columns.Add("GiamGia");
                dt.Columns.Add("ThanhTien");
                dt.Columns.Add("Price");
                dt.Columns.Add("TotalMoney");
                dt.Columns.Add("ProductName");

                int i = 0;
                foreach (DataRow dr in tb.Rows)
                {
                    cmsSanPhamDO obj = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = Int64.Parse(dr[1].ToString()) });
                    i++;
                    DataRow row = dt.NewRow();
                    row[0] = dr[0];
                    row[1] = dr[1];
                    row[2] = obj.TenSanPham;
                    row[3] = obj.HinhAnh;
                    row[4] = dr[2];
                    row[5] = string.Format("{0:0,0} VNÐ", obj.DonGia);
                    row[6] = obj.GiamGia;
                    row[7] = dr[3];
                    row[8] = string.Format("{0:0,0} USD", obj.Price);
                    row[9] = dr[4];
                    row[10] = obj.ProductName;
                    dt.Rows.Add(row);
                }

                if (dt.Rows.Count > 0)
                {
                    GridGioHang.DataSource = dt;
                    GridGioHang.DataBind();
                    float sum = 0;
                    float totalMoney = 0;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        Label lb = GridGioHang.Rows[j].FindControl("lbThanhTien") as Label;
                        if (Session["lang"].ToString() == "VN")
                        {
                            int s = int.Parse(dt.Rows[j].ItemArray[7].ToString());
                            lb.Text = s.ToString("##,## VNĐ");
                        }
                        else
                        {
                            int s = int.Parse(dt.Rows[j].ItemArray[7].ToString());
                            lb.Text = s.ToString("##,## USD");
                        }
                        sum += float.Parse(dt.Rows[j].ItemArray[7].ToString());
                        totalMoney += float.Parse(dt.Rows[j].ItemArray[9].ToString());
                    }
                    if (Session["lang"].ToString() == "VN")
                    {
                        lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                        lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
                    }
                    else
                    {
                        lblSum.Text = "Total money:<span style='color:red;font-weight:bold;'>" + totalMoney.ToString("##,## USD") + "</span>";
                    }

                  
                }
            }
            else
            {
                if (Session["lang"].ToString() == "VN")
                { lbl.Text = "Chua có sản phẩm nào trong giỏ hàng"; }
                else
                {
                    lbl.Text = "No product in cart";
                }

                lblSum.Text = "";
            }
        }
        protected void GridGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int64 id = Int64.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "xoa")
            {
                new cmsGioHangBL().Delete(new cmsGioHangDO { GioHangID = id });
                Response.Redirect("/GioHang.aspx");
            }
            if (e.CommandName == "capnhat")
            {
                cmsGioHangDO obj = new cmsGioHangDO();
                for (int i = 0; i < GridGioHang.Rows.Count; i++)
                {
                    TextBox lbSL = (TextBox)GridGioHang.Rows[i].FindControl("txtSL");

                    obj = new cmsGioHangBL().Select(new cmsGioHangDO { GioHangID = Int64.Parse(e.CommandArgument.ToString()) });
                    cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = obj.SanPhamID });
                    float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                    try
                    {
                        obj.SoLuong = int.Parse(lbSL.Text);
                        obj.ThanhTien = (float)gia * obj.SoLuong;
                    }
                    catch { }
                    new cmsGioHangBL().Update(obj);
                }
                Response.Redirect("/GioHang.aspx");
            }
        }
        protected void GridGioHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
        protected void GridGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Page.Title = new sysConfigBL().Select(new sysConfigDO { ConfigID = 1 }).ConfigValue.ToString();
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Page.Title = new sysConfigBL().Select(new sysConfigDO { ConfigID = 1 }).ConfigValue.ToString();
                    decimal sum = 0;
                    decimal dg = 0;
                    decimal totalmoney = 0;
                    TextBox lbSL = (TextBox)e.Row.FindControl("txtSL");
                    Label DG = (Label)e.Row.FindControl("lbDG");
                    Label GG = (Label)e.Row.FindControl("lbDGG");
                    Label TT = (Label)e.Row.FindControl("lbThanhTien");
                    int sl = int.Parse(lbSL.Text);
                    int gg = int.Parse(GG.Text.Replace("%", "").Trim());
                    if (Session["lang"].ToString() == "VN")
                    {
                        string tem = DG.Text.Replace("VNÐ", "").Trim();
                        tem = tem.Replace(".", "");
                        dg = Decimal.Parse(tem);
                        TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## VNÐ");
                    }
                    if (Session["lang"].ToString() == "EN")
                    {
                        string tem = DG.Text.Replace("USD", "").Trim();
                        tem = tem.Replace(".", "");
                        dg = Decimal.Parse(tem);
                        TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## USD");
                    }


                    for (int i = 0; i < GridGioHang.Rows.Count; i++)
                    {
                        Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                        if (Session["lang"].ToString() == "VN")
                        {
                            sum += Decimal.Parse(TT1.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                        }
                        if (Session["lang"].ToString() == "EN")
                        {
                            totalmoney += Decimal.Parse(TT1.Text.Replace(" USD", "").Replace(".", "").Trim());
                        }
                    }
                    if (Session["lang"].ToString() == "VN")
                    {
                        DG.Text = dg.ToString("##,## VNÐ");
                        lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                        lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
                    }
                    if (Session["lang"].ToString() == "EN")
                    {
                        lblSum.Text = "Total money: <span style='color:red;font-weight:bold;'>" + totalmoney.ToString("##,## USD") + "</span>";
                    }
                }

                if (e.Row.RowType == DataControlRowType.Footer)
                {
                }
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("style", "background-color: #FFFFFF; color: black;");
                    e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#DEE0BF'");
                    e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#FFFFFF'");
                }
            }
            //catch { }

        }
        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            for (int i = 0; i < GridGioHang.Rows.Count; i++)
            {
                TextBox lbSL = (TextBox)GridGioHang.Rows[i].FindControl("txtSL");
                //int valuuu = (int)GridGioHang[i, 2].Value;
                obj = new cmsGioHangBL().Select(new cmsGioHangDO { GioHangID = Int64.Parse(GridGioHang.DataKeys[i].Value.ToString()) });
                try
                {
                    obj.SoLuong = int.Parse(lbSL.Text);
                }
                catch { }
                new cmsGioHangBL().Update(obj);
            }
            loadGioHang();
        }
        protected void GridGioHang_DataBinding(object sender, System.EventArgs e)
        {

        }
        protected void GridGioHang_DataBound(object sender, EventArgs e)
        {

        }

        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        protected void lblThanhToan_Click(object sender, EventArgs e)
        {
            if (Session["IDDN"] != null)
            {
                Response.Redirect("/ThanhToan.aspx");
            }
            else
            {
                Response.Redirect("/checkout.aspx");
            }
        }

    }
}
